package cn.study.cas.unsafe;

import sun.misc.Unsafe;

/**
 * @Author     ：跟大佳一起学架构
 * @Description：申请堆外内存分配
 */
public class AllocateMemoryAccess {

    public static void main(String[] args) {
        Unsafe unsafe = UnsafeInstance.reflectGetUnsafe();
        long oneHundred = 123456789123456789L;
        byte size = 8;
        /*
         * 调用allocateMemory分配内存
         */
        long memoryAddress = unsafe.allocateMemory(size);
        System.out.println("address:->"+memoryAddress);
        /*
         * 将1写入到内存中
         */
        unsafe.putAddress(memoryAddress, oneHundred);
        /*
         * 内存中读取数据
         */
        long readValue = unsafe.getAddress(memoryAddress);
        System.out.println("value : " + readValue);
        unsafe.freeMemory(memoryAddress);
    }

}
